//
//  Service+Content.m
//  huimei
//
//  Created by fengyn on 16/1/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "Service+Content.h"
#define kSearchHistoryKey @"contentSearchHistory"
@implementation ServiceManager(Content)
///根据帖子查询帖子详情
+ (void)getContentDetailInfoWithId:(NSString *)theId success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure{
    NSString *path = [NSString stringWithFormat:@"/content/topic/%@",theId];
    NSDictionary *parameterDict=nil;
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///根据帖子类型分页查询帖子
+ (void)getContentWithType:(NSString *)contentType pageIndex:(NSUInteger)pageIndex success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure{
    NSString *path = [NSString stringWithFormat:@"/content/portal/%@",contentType];
    const int countPer=20;
    NSDictionary *parameterDict=@{@"start":@(pageIndex*countPer),
                                  @"length":@(countPer)};
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

+ (void)searchContentWithString:(NSString *)stringValue type:(NSString *)type pageIndex:(NSUInteger)pageIndex success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure{
    [self addToHistoryWithStringKeywords:stringValue];
    NSString *path = [NSString stringWithFormat:@"/content/publicuser/search/%@",type];
    const int countPer=20;
    NSDictionary *parameterDict=@{@"start":@(pageIndex*countPer),
                                  @"length":@(countPer),
                                  @"keyWord":stringValue};
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///获取热门搜索
+ (void)getHotSearchSuccess:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/content/hotsearch";
    NSDictionary *parameterDict=nil;
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///获取搜索历史记录
+ (NSArray *)getSearchHistory{
    NSArray *historyArray =[[NSUserDefaults standardUserDefaults] objectForKey:kSearchHistoryKey];
    if (historyArray&&historyArray.count>6) {
        historyArray=[historyArray subarrayWithRange:NSMakeRange(0,6)];
    }
    return historyArray;
}

///将搜索内容添加到Hisory
+ (void)addToHistoryWithStringKeywords:(NSString *)stringKeyworks{
    NSMutableArray *historyArray=[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:kSearchHistoryKey]];
    [historyArray removeObject:stringKeyworks];
    [historyArray insertObject:stringKeyworks atIndex:0];
    [[NSUserDefaults standardUserDefaults] setObject:historyArray forKey:kSearchHistoryKey];
}

///发布问题
+ (void)publishQuestionWithTitle:(NSString *)title body:(NSString *)body categoryCode:(NSString *)categoryCode imageUrls:(NSArray<NSString *> *)imageUrls success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/content/question";
    NSDictionary *parameterDict=@{@"contentTitle":title,
                                  @"contentBody":body,
                                  @"categoryCode":categoryCode,
                                  @"images":imageUrls};
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///发布专栏
+ (void)publishCollumnWithTitle:(NSString *)title body:(NSString *)body imageUrls:(NSArray<NSString *> *)imageUrls success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/content/topic";
    NSDictionary *parameterDict=@{@"contentTitle":title,
                                  @"contentBody":body,
                                  @"contentLabelType":@"COLUMN",
                                  @"images":imageUrls};
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///发布SayHi
+ (void)publishSayHiWithTitle:(NSString *)title body:(NSString *)body imageUrls:(NSArray<NSString *> *)imageUrls success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/content/topic";
    NSDictionary *parameterDict=@{@"contentTitle":title,
                                  @"contentBody":body,
                                  @"contentLabelType":@"SAYHI",
                                  @"images":imageUrls};
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///发布日记
+ (void)publishDiaryWithDiaryBookId:(NSString *)diaryBookId date:(NSString *)publishDate body:(NSString *)body imageUrls:(NSArray<NSString *> *)imageUrls success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/content/diary";
    NSDictionary *parameterDict=@{@"diarySequenceNbr":diaryBookId,
                                  @"publishDate":publishDate,
                                  @"contentBody":body,
                                  @"images":imageUrls};
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}
///查询我的专栏
+ (void)getMySpecialColumnWithType:(NSString *)type pageIndex:(NSInteger)pageIndex success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/content/topic/publicuser/me";
    const int countPer=20;
    NSDictionary *parameterDict=@{@"start":@(pageIndex*countPer),
                                  @"length":@(countPer),
                                  @"businessType":type,
                                  @"contentLabelType":@"COLUMN"};
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];

}



///查询我的日记，sayHi，提问
+ (void)getMySayHiWithType:(NSString*)type pageIndex:(NSInteger)pageIndex success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/content/topic/publicuser/me";
    const int countPer=20;
    NSDictionary *parameterDict=@{@"start":@(pageIndex*countPer),
                                  @"length":@(countPer),
                                  @"businessType":type};
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///根据ID删除帖子
+ (void)deleteContentWithId:(NSString *)theId success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = [NSString stringWithFormat:@"/content/%@",theId];
    NSDictionary *parameterDict=nil;
    [NetworkClient deleteRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///帖子Key
+ (NSString *)contentKeyWithId:(NSString *)newsId
{
    return [NSString stringWithFormat:@"contentID%@",newsId];
}

///是否已对资讯点过赞
+ (BOOL)isUpvotedContentWithID:(NSString *)newsId
{
    BOOL isValue = NO;
    NSString *newsKey=[self contentKeyWithId:newsId];
    NSString *newsValue=[[NSUserDefaults standardUserDefaults] objectForKey:newsKey];
    if (newsValue) {
        isValue=YES;
    }
    return isValue;
}

///对帖子点赞
+ (void)upvoteContentWithId:(NSString *)theId success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    if ([self isUpvotedContentWithID:theId]) {
        DLog(@"已经对该资讯点过赞");
        return;
    }
    NSString *newsKey=[self contentKeyWithId:theId];
    [[NSUserDefaults standardUserDefaults] setObject:newsKey forKey:newsKey];
    
    NSString *path = [NSString stringWithFormat:@"/content/upvote/%@",theId];
    NSDictionary *parameterDict=nil;
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

//发布自定义模板库
+ (void)publishTemplateWithCategoryCode:(NSString *)categoryCode templateTitle:(NSString *)templateTitle tempalteContent:(NSString *)tempalteContent uccess:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = [NSString stringWithFormat:@"/replytemplate/public"];
    NSDictionary * parameterDict = @{@"categoryCode":categoryCode,
                                     @"templateTitle":templateTitle,
                                     @"tempalteContent":tempalteContent};
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

@end
